package yf.mall.service.impl;

import com.yf.service.impl.BaseServiceImpl;
import com.yf.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import yf.mall.dao.CourseDao;
import yf.mall.dao.UserDao;
import yf.mall.entity.CourseEntity;
import yf.mall.entity.UserEntity;
import yf.mall.service.NoticeService;
import yf.mall.vo.SignVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import yf.mall.dao.SignDao;
import yf.mall.entity.SignEntity;
import yf.mall.service.SignService;

/**
 * 报名Service实现类
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Mon Oct 21 08:39:58 CST 2019
 */
@Service
@Slf4j
public class SignServiceImpl extends BaseServiceImpl<SignEntity> implements SignService {

    private SignDao signDao;


    @Autowired
    public void setDao(SignDao signDao) {
        this.signDao = signDao;
        super.setBaseDao(signDao);
    }


    /**
     *
     *   @Override
     *     public List<UserVo> queryListVo(UserVo userVo) {
     *         return userDao.queryListVo(userVo);
     *     }
     *
     *     @Override
     *     public UserVo getVo(Long id) {
     *         return userDao.getVo(id);
     *     }

     @Override List<SignVo> queryListVo(SignVo signVo){
     return signDao.queryListVo(signVo);
     }

     @Override SignVo getVo(Long id){
     return signDao.getVo(id);
     }
     **/


    /**内部方法**/


    /**
     * 外部方法实现
     **/

    @Override
    public List<SignVo> findByUserId(Long userId) {
        return signDao.findByUserId(userId);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
    public void active(List<Long> signIds) {
        signIds.forEach(signId -> {
            SignEntity signEntity = signDao.get(signId);
            if (signEntity.getActive()) {
                return;
            }
            //激活
            signEntity.setActive(true);
            UserEntity userEntity = userDao.get(signEntity.getUserId());
            CourseEntity courseEntity = courseDao.get(signEntity.getCourseId());
            signDao.update(signEntity);

                noticeService.sendConfirmCourse(userEntity.getOpenId(), courseEntity.getName(), DateUtil.dateToStr(courseEntity.getTime()) + courseEntity.getAddress());

        });
    }

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private UserDao userDao;

    @Autowired
    private CourseDao courseDao;
}
